package io.bdmc.modules.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.bdmc.common.utils.*;
import io.bdmc.common.validator.Assert;
import io.bdmc.modules.api.service.DeviceStatusService;
import io.bdmc.modules.bss.entity.*;
import io.bdmc.modules.bss.model.DeviceAlarmParam;
import io.bdmc.modules.bss.model.DeviceAlarmRealtimeParam;
import io.bdmc.modules.bss.model.DeviceMeasureParam;
import io.bdmc.modules.bss.model.DeviceReportParam;
import io.bdmc.modules.bss.service.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

// "北向接口", description = "/api/v1/north")
@RestController
@RequestMapping(value = "/api/v2/north")
public class NorthController {

    @Autowired
    IDeviceDeviceService _devSvs;

    @Autowired
    IFibreFibreService _fibreSvs;

    @Autowired
    IFibreCablePointService _pointSvs;

    @Autowired
    IDeviceEquipmentroomService _roomSvs;

    @Autowired
    IDeviceFrameService _frameSvs;

    @Autowired
    IDeviceAlarmService _alarmSvs;

    @Autowired
    IDeviceAlarmRealtimeService _alarmRealSvs;

    @Autowired
    IDeviceFibreRealtimeService _dfrSvs;

    @Autowired
    IDeviceFibreMatchService _dfmSvs;
    @Autowired
    IDeviceMeasureService _measureSvs;

    @Autowired
    IDeviceMeasureEventService _eventService;

    @Autowired
    IDeviceRouteGroupService _routeGroupService;

    @Autowired
    IDeviceRouteService _routeService;

    @Autowired
    IDeviceRouteConfigService _routeConfigService;

    @Autowired
    IDeviceRouteAlarmSettingService _routeAlarmSettingService;

    @Autowired
    IDeviceLinkService _linkService;

    @Autowired
    IDeviceLinkGroupService _linkGroupService;

    @Autowired
    DeviceStatusService deviceStatusSvs;

    // "设备信息同步", notes = "设备信息同步")
    @GetMapping(value = "/deviceSync")
    public R deviceSync() throws Exception {
        List<DeviceDevice> list = _devSvs.listAllDevices();
        return R.ok().putData(list);
    }

    // "设备端口与光纤芯信息同步", notes = "设备端口与光纤芯信息同步")
    @GetMapping(value = "/channelSync")
    public R SyncChannel() {
        return R.ok().putData(_dfmSvs.list(new QueryWrapper<DeviceFibreMatch>().isNotNull("device_sn")));
    }

    // "光缆和设施点信息", notes = "光缆和设施点信息")
    @GetMapping(value = "/fibreSync")
    public R fibreSync() {
        List<FibreFibre> fibreList = _fibreSvs.list();
        for (FibreFibre fibreFibre : fibreList) {
            fibreFibre.setFibreCablePointList(
                    _pointSvs.list(new QueryWrapper<FibreCablePoint>().eq("fibre_id", fibreFibre.getFibreId())));
        }
        return R.ok().putData(fibreList);
    }

    // "同步所有机房信息和机架信息", notes = "同步所有机房信息和机架信息")
    @GetMapping(value = "/equipmentroomSync")
    public R equipmentroomSync() {
        List<DeviceEquipmentroom> roomList = _roomSvs.list();
        for (DeviceEquipmentroom room : roomList) {
            room.setFrameList(
                    _frameSvs.list(new QueryWrapper<DeviceFrame>().eq("equipmentroom_id", room.getEquipmentroomId())));
        }
        return R.ok().putData(roomList);
    }

    // "历史报警信息同步接口", notes = "历史报警信息同步接口")
    @GetMapping(value = "/alarmHistorySync")
    public R alarmHistorySync(DeviceAlarmParam params) {
        MyPage<DeviceAlarm> myPage = new MyPage<>(params.getCurrent(), params.getSize());
        params.setDataType("ALARM");
        MyPage<DeviceAlarm> map = _alarmSvs.mySelectPagePermitAll(myPage, params);
        return R.ok().putData(MyMap.ok(map.getRecords(), map.getTotal()));

    }

    // "实时报警信息同步接口", notes = "实时报警信息同步接口")
    @GetMapping(value = "/alarmRealtimeSync")
    public R alarmRealtimeSync(DeviceAlarmRealtimeParam params) {
        MyPage<DeviceAlarmRealtime> myPage = new MyPage<>(params.getCurrent(), params.getSize());
        params.setDataType("ALARM");
        MyPage<DeviceAlarmRealtime> map = _alarmRealSvs.mySelectPagePermitAll(myPage, params);
        return R.ok().putData(MyMap.ok(map.getRecords(), map.getTotal()));
    }

    // "设备实时长度衰耗信息同步接口", notes = "设备实时长度衰耗信息同步接口")
    @GetMapping(value = "/fibreLengthSync")
    public R fibreLengthSync(String ipaddr) {
        QueryWrapper<DeviceFibreRealtime> w1 = new QueryWrapper<>();
        w1.eq("ip_addr", ipaddr);
        List<DeviceFibreRealtime> dfr = _dfrSvs.list(w1);
        return R.ok().putData(dfr);
    }

    // "设备测量结果信息同步接口", notes = "设备测量结果信息同步接口")
    @GetMapping(value = "/deviceMeasureSync")
    public R deviceMeasureSync(DeviceMeasureParam params) {
        MyPage<DeviceMeasure> myPage = new MyPage<DeviceMeasure>(params.getCurrent(), params.getSize());
        MyPage<DeviceMeasure> map = _measureSvs.mySelectPage1(myPage, params);
        List<DeviceMeasure> result = map.getRecords();
        for (DeviceMeasure measure : result) {
            QueryWrapper<DeviceMeasureEvent> w1 = new QueryWrapper<>();
            w1.eq("measure_id", measure.getMeasureId());
            measure.setEvents(_eventService.list(w1));
        }
        return R.ok().putData(MyMap.ok(result, map.getTotal()));
    }

    /**
     * 路由试图同步接口
     *
     * @return 路由相关所有信息
     */
    @GetMapping(value = "/routingviewSync")
    public R routingviewSync() {
        List<DeviceRouteGroup> routeGroupList = _routeGroupService.list();
        for (DeviceRouteGroup deviceRouteGroup : routeGroupList) {
            final List<DeviceRoute> deviceRouteList = _routeService
                    .list(new QueryWrapper<DeviceRoute>().eq("group_id", deviceRouteGroup.getRouteGroupId()));
            for (DeviceRoute route : deviceRouteList) {
                route.setDeviceRouteConfigList(_routeConfigService.selectByRouteId(route.getRouteId()));
                route.setDeviceRouteAlarmSettingList(_routeAlarmSettingService.selectByRouteId(route.getRouteId()));
            }
            deviceRouteGroup.setDeviceRouteList(deviceRouteList);
        }
        return R.ok().putData(routeGroupList);
    }

    /**
     * 链路视图同步接口
     *
     * @return 链路相关信息
     */
    @GetMapping(value = "/linkviewSync")
    public R linkviewSync() {
        final List<DeviceLinkGroup> linkGroupList = _linkGroupService.list();
        for (DeviceLinkGroup deviceLinkGroup : linkGroupList) {
            final List<DeviceLink> deviceLinkList = _linkService
                    .list(new QueryWrapper<DeviceLink>().eq("group_id", deviceLinkGroup.getLinkGroupId()));
            deviceLinkGroup.setDeviceLinkList(deviceLinkList);
        }
        return R.ok().putData(linkGroupList);
    }

    @GetMapping(value = "devicestateSync")
    public R devicestateSync(String deviceSn) {
        Assert.isBlank(deviceSn, "deviceSn未填写正确");
        DeviceDevice dd = _devSvs.getDeviceBySn(deviceSn);
        if (dd == null) {
            return R.error(-1, "设备不存在或已移除");
        }
        return R.ok().putData(deviceStatusSvs.getDeviceStatus(dd));
    }

    @GetMapping("devicehisstateSync")
    public R devicehisstateSync(DeviceReportParam reportParam) {
        Assert.isNull(reportParam.getSize(), "size未填写正确");
        Assert.isNull(reportParam.getCurrent(), "current未填写正确");
        Assert.isBlank(reportParam.getDeviceSn(), "deviceSn未填写正确");
        final DeviceDevice device = _devSvs.getDeviceBySn(reportParam.getDeviceSn());
        Assert.isNull(device, "未找到指定设备：" + reportParam.getDeviceSn());
        MyMap<String, Object> deviceHistoryStatus = deviceStatusSvs.getDeviceHistoryStatus(device, reportParam);
        return R.ok().putData(deviceHistoryStatus);
    }
}
